package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.model.dto.PipeYearsTjDTO;
import com.antoco.operation.bigscreen.model.entity.PipeYearsTj;
import com.antoco.operation.bigscreen.controller.modelcontroller.intf.PipeYearsTjController;
import com.antoco.operation.bigscreen.mapper.PipeYearsTjMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.PipeYearsTjService;
import org.springframework.data.domain.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RequestMapping("/model/pipe_years_tj")
@RestController
public class PipeYearsTjControllerImpl implements PipeYearsTjController {

    @Autowired
    private PipeYearsTjService pipeYearsTjService;

    @Autowired
    private PipeYearsTjMapper pipeYearsTjMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public PipeYearsTjDTO save(@Validated @RequestBody PipeYearsTjDTO pipeYearsTjDTO) {
        PipeYearsTj pipeYearsTj = pipeYearsTjMapper.asEntity(pipeYearsTjDTO);
        pipeYearsTjService.save(pipeYearsTj);
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeYearsTjDTO;
    }

    @Override
    @GetMapping("/{id}")
    public PipeYearsTjDTO findById(@PathVariable("id") String id) {
        PipeYearsTj pipeYearsTj = pipeYearsTjService.findById(id).orElse(null);
        return pipeYearsTjMapper.asDTO(pipeYearsTj);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        pipeYearsTjService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<PipeYearsTjDTO> list() {
        return pipeYearsTjMapper.asDTOList(pipeYearsTjService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<PipeYearsTjDTO> pageQuery(Pageable pageable) {
        Page<PipeYearsTj> pipeYearsTjPage = pipeYearsTjService.findAll(pageable);
        List<PipeYearsTjDTO> dtoList = pipeYearsTjPage
                .stream()
                .map(pipeYearsTjMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, pipeYearsTjPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public PipeYearsTjDTO update(@RequestBody PipeYearsTjDTO pipeYearsTjDTO, @PathVariable("id") String id) {
        PipeYearsTj pipeYearsTj = pipeYearsTjMapper.asEntity(pipeYearsTjDTO);
        pipeYearsTjMapper.asDTO(pipeYearsTjService.update(pipeYearsTj, id));
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeYearsTjDTO;
    }
}